package Tests;

import junit.framework.Assert;

import org.jmock.Mockery;
import org.junit.Test;

import Core.Map.CombatGridGraph;
import Core.Map.DekalbPathFinderImpl;
import Core.Map.Path;
import Core.Map.Tile;
import Core.Map.TileVertex;
import Core.Character;

public class DekalbPathfinderTests
{
	@Test
	public void findsSimplePath()
	{
		Mockery context = new Mockery();
		
		final Character character = context.mock(Character.class);
		
		CombatGridGraph graph = this.buildTestGridGraph();
		
		
		DekalbPathFinderImpl dekalbFinder = new DekalbPathFinderImpl();
		
		Path path = dekalbFinder.calculateShortestPath(graph, character, 1, 1, 1, 3);
		
		Assert.assertEquals(2, path.tiles().length);
		Assert.assertTrue(checkTileXAndY(path.getTileAtStep(1), 1, 2));
		Assert.assertTrue(checkTileXAndY(path.getTileAtStep(1), 1, 3));
	}
	
	private boolean checkTileXAndY(Tile tile, int expectedX, int expectedY)
	{
		return tile.getX() == expectedX && tile.getY() == expectedY; 
	}
	
	private CombatGridGraph buildTestGridGraph()
	{
		CombatGridGraph testGraph = new CombatGridGraph();
		
		int maxX = 3;
		int maxY = 3;
		
		for (int currentX = 1; currentX <= maxX; currentX++)
		{
			for (int currentY = 1; currentY <= maxY; currentY++)
			{
				testGraph.addVertex(new TileVertex(new Tile(currentX, currentY)));
			}
		}

		// 1,1
		testGraph.addEdge(1, 1, 1, 2, 1.0);
		testGraph.addEdge(1, 1, 2, 2, 1.5);
		testGraph.addEdge(1, 1, 2, 1, 1.0);
		
		// 1,2
		testGraph.addEdge(1, 2, 1, 3, 1.0);
		testGraph.addEdge(1, 2, 2, 1, 1.5);
		testGraph.addEdge(1, 2, 2, 2, 1.0);
		testGraph.addEdge(1, 2, 2, 3, 1.5);
		
		// 1,3
		testGraph.addEdge(1, 3, 2, 2, 1.5);
		testGraph.addEdge(1, 3, 2, 3, 1.0);
		
		// 2,1
		testGraph.addEdge(2, 1, 2, 2, 1.0);
		testGraph.addEdge(2, 1, 3, 1, 1.0);
		testGraph.addEdge(2, 1, 3, 2, 1.5);
		
		// 2,2
		testGraph.addEdge(2, 2, 2, 3, 1.0);
		testGraph.addEdge(2, 2, 3, 1, 1.5);
		testGraph.addEdge(2, 2, 3, 2, 1.0);
		testGraph.addEdge(2, 2, 3, 3, 1.5);
		
		// 2,3
		testGraph.addEdge(2, 3, 3, 2, 1.5);
		testGraph.addEdge(2, 3, 3, 3, 1.0);
		
		// 3,1
		testGraph.addEdge(3, 1, 3, 2, 1.0);
		
		// 3,2
		testGraph.addEdge(3, 2, 3, 3, 1.0);
		
		return testGraph;
	}
}
